﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Net;

namespace intelliEssayBugCheckSystem
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            Application.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);
            try
            {
                HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create("http://www.baidu.com");
                hwr.Timeout = 1000;
                hwr.GetResponse();
            }
            catch
            {
                ApplicationStates.conState = ConnectionStates.Disconnected;
            }
          
            ApplicationStates.CurrentUserName = ApplicationStates.GetUserNameFromAllHints();
            ApplicationStates.CurrentUserID = ApplicationStates.GetUserIDFromUserName(ApplicationStates.CurrentUserName);
            intelliEssay.ApplicationExceptionReportingRecordingRedistributing.BugCollection.LoadBugCollection(ApplicationStates.CurrentUserName); 
            AutomaticMechanism.Auto();
            System.IO.File.Create("Engage" + ApplicationStates.CurrentUserName);
        }

        void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            MessageBox.Show(e.Exception.ToString());
        }

        private void Application_Exit(object sender, ExitEventArgs e)
        {
            ApplicationStates.LockIO();
            AutomaticMechanism.MergeFilesEx();
            ApplicationStates.ReleaseIO();
            try
            {
                System.IO.File.Delete("Engage" + ApplicationStates.CurrentUserName);
            }
            catch
            {
                MessageBox.Show("Engage File is corrputed - states may be lost");
            }
        }
    }
}
